package org.ourgrid.acceptance.discoveryservice;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.ourgrid.acceptance.util.discoveryservice.Req_502_Util;
import org.ourgrid.common.interfaces.CommunityStatusProvider;
import org.ourgrid.discoveryservice.DiscoveryServiceComponent;
import org.ourgrid.discoveryservice.DiscoveryServiceConstants;
import org.ourgrid.reqtrace.ReqTest;


public class Req_502_Test extends DiscoveryServiceAcceptanceTestCase {
	
		Req_502_Util req_502_Util = new Req_502_Util(getComponentContext());
		
		/**
		 * Create a Discovery Service;
		 * Start the DS with the public key "wrongPublicKey" - Verify if the following warn message was logged:
          	o An unknown entity tried to start the Discovery Service. Only the local modules can perform this operation. Unknown entity public key: [senderPublicKey].
		 * @throws Exception
		 */
		@ReqTest(test = "AT-502.1", reqs = "")
		@Test public void test_AT_502_1_UnknownEntitySendAStartCommand() throws Exception{
			// Start the Discovery Service with the public key "wrongPublicKey"
			req_502_Util.startDiscoveryServiceWithWrongPublicKey("wrongPublicKey");
		}
		
		/**
		 * Create a DS;
		 * Start a DS with the correct public key;
    	 * Verify if the following message was logged:
          	o "Discovery Service has been successfully started."
		 * Get(lookup) the remote object "COMMUNITY_STATUS_PROVIDER" and verify if its type is:
          	o org.ourgrid.common.interfaces.CommunityStatusProvider
		 * Verify if the ControlClient received the operation succeed message.
		 * @throws Exception
		 */
		@ReqTest(test = "AT-502.2", reqs = "")
		@Test public void test_AT_502_2_StartCommand() throws Exception{
			// Start the Discovery Service
			DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();
			
			// Get(lookup) the remote object "COMMUNITY_STATUS_PROVIDER"
			assertTrue(isBound(component, DiscoveryServiceConstants.COMMUNITY_STATUS_PROVIDER, 
					CommunityStatusProvider.class));
		}
		
		/**
		 * Create a DS;
		 * Start a DS with the correct public key;
		 * Start the same DS again with the correct public key.
		 * Verify if the Control Result Operation contains an exception whose type is:
          	o br.edu.ufcg.lsd.commune.container.control.ComponentAlreadyStartedException
		 * @throws Exception
		 */
		@ReqTest(test = "AT-502.3", reqs = "")
		@Test public void test_AT_502_3_TryToStartAAlreadyStartedDS() throws Exception{
			// Start the Discovery Service
			DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();
			
			// Start the Discovery Service again
			req_502_Util.startDiscoveryServiceAgain(component);
		}
}
